﻿using System.IO;
using FrameWork.Core.Extend;

namespace FrameWork.Core.Utils
{
    public static class FileHelper
    {
        static object obj = new object();
        /// <summary>
        /// 通过路径获取字符串
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string GetStr(string path)
        {
            lock (obj)
            {
                FileStream fileStream = null;
                StreamReader streamReader = null;
                try
                {
                    fileStream = new FileStream(path, FileMode.OpenOrCreate);
                    streamReader = new StreamReader(fileStream);
                    var str = streamReader.ReadToEnd();
                    return str;
                }
                finally
                {
                    if (streamReader != null)
                    {
                        streamReader.Close();
                    }
                    if (fileStream != null)
                    {
                        fileStream.Close();
                    }
                }
            }
        }
        /// <summary>
        /// 保存字符串到指定路径
        /// </summary>
        /// <param name="path"></param>
        /// <param name="str"></param>
        /// <param name="fileMode"></param>
        public static void SaveStr(string path, string str, FileMode fileMode = FileMode.Create)
        {
            lock (obj)
            {
                FileStream fileStream = null;
                if (!File.Exists(path)) fileStream = File.Create(path);
                using (fileStream = fileStream ?? new FileStream(path, fileMode))
                {
                    using (StreamWriter streamWriter = new StreamWriter(fileStream))
                    {
                        streamWriter.WriteLine(str);
                        streamWriter.Flush();
                        streamWriter.Close();
                        fileStream.Close();
                    }
                }
            }
        }
        /// <summary>
        /// 保存文件
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="bytes"></param>
        public static void SaveFile(string filePath, byte[] bytes)
        {
            MemoryStream memoryStream = null;
            FileStream fileStream = null;
            try
            {

                var directoy = Path.GetDirectoryName(filePath);
                if (Directory.Exists(directoy) == false)
                    Directory.CreateDirectory(directoy);
                memoryStream = new MemoryStream(bytes);
                fileStream = new FileStream(filePath, FileMode.Create);
                memoryStream.WriteTo(fileStream);
            }
            finally
            {
                memoryStream.Close();
                memoryStream = null;

                fileStream.Close();
                fileStream = null;
            }
        }
        /// <summary>
        /// 保存文件
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="path"></param>
        /// <param name="data"></param>
        public static void SaveJsonFile<T>(string path, T data)
        {
            var json = data.ToJsonString();
            File.WriteAllText(path, json);
        }
        /// <summary>
        /// 加载文件
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="path"></param>
        /// <returns></returns>
        public static T LoadJsonFile<T>(string path)
        {
            var json = File.ReadAllText(path);
            return json.ToJsonObject<T>();
        }
    }
}